package br.mikhas.util.json;

import java.util.HashMap;
import java.util.Map;

public class JSONObject implements JSONable {

	/**
	 * Holds object properties/attributes
	 */
	private Map<String, Object> map = new HashMap<String, Object>();

	/**
	 * Sets the value of a named attribute
	 * 
	 * @param property
	 *            The name od tyhe attribute
	 * @param value
	 *            The value of the attribute
	 */
	public void set(String property, Object value) {
		map.put(property, value);
	}

	/**
	 * Gets the value of a named attribute.
	 * 
	 * @param <T>
	 *            The type of the attribute
	 * @param property
	 *            The name of the attribute
	 * @return The value of the attribute or <code>null</code> if it doesn't
	 *         exists.
	 */
	@SuppressWarnings("unchecked")
	public <T> T get(String property) {
		return (T) map.get(property);
	}

	/**
	 * Creates a JSON representation of the current object.
	 */
	public String toJson() {
		return JSONUtils.toJson(this.map);
	}

	/**
	 * Creates a JSON representation of the current object and appends it on a
	 * string builder.
	 * 
	 * @param builder
	 *            The string builder to append the current object
	 */
	public void toJson(StringBuilder builder) {
		JSONUtils.toJson(this.map, builder);
	}

}
